/**
 * JAVACC DEMO 1.0
 */
package com.apache.ius.function;

import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;
import com.apache.tools.StrUtil;
import com.apache.workday.WorkdayUtils;
import com.greenpineyu.fel.function.CommonFunction;
import com.greenpineyu.fel.function.Function;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * description:
 *
 * @author cbqiu 创建时间：2016-12-12
 */
public class FelFunction {

    private static FelFunction instance;

    private enum DateEnum {
        Default(""), SumWorkDate("sumWorkDate"), SumDate("sumDate"), DateCompare(
                "dateCompare"), SetNowDate("setNowDate"), SetNowTime("setNowTime"), SetNowDateTime(
                "setNowDateTime"), SetLongTime("setLongTime");

        private String name;

        private DateEnum(String name) {
            this.name = name;
        }

        public static DateEnum getName(String name) {
            if (StrUtil.isNull(name))
                return Default;
            for (DateEnum c : DateEnum.values()) {
                if (c.name.equals(name)) {
                    return c;
                }
            }
            return Default;
        }
    }

    private FelFunction() {
    }

    public static FelFunction getInstance() {
        if (null == instance) {
            instance = new FelFunction();
        }
        return instance;
    }

    /**
     * description: 日期计算
     *
     * @return
     */
    public Function dateCalculate() {
        Function fun = new CommonFunction() {

            public String getName() {
                return "dateCalculate";
            }

            public String call(Object[] args) {
                String result = dateScheduling(String.valueOf(args[0]), String.valueOf(args[1]));
                return result;
            }
        };
        return fun;
    }

    /**
     * description: 日期验证
     *
     * @return
     */
    public Function dateValidate() {
        Function fun = new CommonFunction() {

            public String getName() {
                return "dateValidate";
            }

            public String call(Object[] args) {
                String result = dateScheduling(String.valueOf(args[0]), String.valueOf(args[1]));
                return result;
            }
        };
        return fun;
    }

    public String dateScheduling(String... args) {
        String result = "";
        try {
            switch (DateEnum.getName(String.valueOf(args[0]))) {
            case SumWorkDate:
                result = sumDate(String.valueOf(args[1]), true);
                break;
            case SumDate:
                result = sumDate(String.valueOf(args[1]), false);
                break;
            case DateCompare:
                result = dateCompare(String.valueOf(args[1]));
                break;
            case SetNowDate:
                result = DateUtils.Now.fmt_yyyyMMdd();
                break;
            case SetNowTime:
                result = DateUtils.Now.fmt_HHmmss();
                break;
            case SetNowDateTime:
                result = DateUtils.Now.fmt_yyyyMMdd_HHmmss();
                break;
            case SetLongTime:
                result = DateUtils
                        .strToLong(DateUtils.Now.fmt_yyyyMMdd_HHmmss(), FmtStr.yyyyMMdd_HHmmss)
                        + "";
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return result;
    }

    /**
     * description: 日期比较
     *
     * @param str 　如：2015-11-21>=23(即当前时间加上月数(23)是否大于等于2015-11-21)
     */
    private String dateCompare(String str) throws ParseException {
        String msg = "";
        if (Validator.isNotNull(str)) {
            String strs[] = new String[2];
            String eq = "";
            if (str.indexOf(">=") != -1) {
                strs = str.split(">=");
                eq = ">=";
            } else if (str.indexOf("<=") != -1) {
                strs = str.split("<=");
                eq = "<=";
            } else if (str.indexOf(">") != -1) {
                strs = str.split(">");
                eq = ">";
            } else if (str.indexOf("<") != -1) {
                strs = str.split("<");
                eq = "<";
            } else if (str.indexOf("=") != -1) {
                strs = str.split("=");
                eq = "==";
            }
            long eTime = DateUtils.strToLong(strs[0], FmtStr.yyyyMMdd);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date now = sdf.parse(DateUtils.Now.fmt_yyyyMMdd());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.MONTH, Integer.valueOf(strs[1]));
            long cTime = DateUtils
                    .strToLong(DateUtils.format(calendar.getTime(), "yyyy-MM-dd"), FmtStr.yyyyMMdd);
            msg = eTime + eq + cTime;
        }
        return msg;
    }

    /**
     * description:  计算日期(加几天)
     *
     * @param arguments 如2019-10-11+10
     */
    private String sumDate(String arguments, boolean isWorkDate)
            throws NumberFormatException, Exception {
        String msg = "";
        if (Validator.isNotNull(arguments)) {
            String strs[] = arguments.split("\\+");
            if (Validator.isNull(strs[0])) {
                strs[0] = DateUtils.Now.fmt_yyyyMMdd();
            }
            if (strs[0].length() > 10)
                strs[0] = strs[0].substring(0, 11);
            if (isWorkDate) {
                msg = WorkdayUtils.getInstance().afterWorkday(strs[0], Integer.valueOf(strs[1]));
                msg = DateUtils.format(DateUtils.parse(msg, "yyyyMMdd"), "yyyy-MM-dd");
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date now = sdf.parse(strs[0]);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(now);
                calendar.add(Calendar.DATE, Integer.valueOf(strs[1]));
                msg = DateUtils.format(calendar.getTime(), "yyyy-MM-dd");
            }
        }
        return msg;
    }

    public static void main(String[] args) {
        String result = FelFunction.getInstance().dateScheduling("certEndTime", "2015-11-21>=23");
        System.out.println(result);
        String sql = "sumDate:2015-11-21+23";
        String str[] = sql.split(":");
        result = FelFunction.getInstance().dateScheduling(str[0], "2015-11-21+23");
        System.out.println(result);
    }
}
